<?php

FLEA::loadClass ( 'Common_Action' );

class Controller_AdminRoles extends Common_Action {
	
	private $_modelRoles = null;
	
	function __construct($ctlName) {
		parent::__construct ( $ctlName );
		$this->tplDir = TPL_A;
		$this->_modelRoles = FLEA::getSingleton ( 'Model_Roles' );
	}
	
	function index() {
		$viewData = $this->_modelRoles->getAllRoles ();
		$this->_out ( 'listRoles', $viewData );
	}
	
	function insertRole() {
		if ($this->_isPOST ()) {
			$result = $this->_modelRoles->insertRole ( $_POST );
			if ($result != NULL) {
				js_alert ( '添加成功', null, url ( 'adminroles' ) );
			}
		} else {
			$viewData = array ('form' => $this->_modelRoles->getForm (), 'action' => 'insertRole' );
			$this->_out ( 'insertRole', $viewData );
		}
	}
	
	function modifyRole() {
		if ($this->_isPOST ()) {
			$result = $this->_modelRoles->updateRole($_POST);
			if ($result != NULL) {
				js_alert ( '修改成功', null, url ('adminroles') );
			}	
		} else {
			$roleId = $this->saveGet ( 'role' );
			$role = $this->_modelRoles->getRoleById ( $roleId );
			$viewData = array ('form' => $this->_modelRoles->getForm ( $role ), 'action' => 'modifyRole' );
			//dump($viewData);
			$this->_out ( 'insertRole', $viewData );
		}
	}
	
	function deleteRole() {
		//询问是否删除
		$roleId = $this->saveGet ( 'role' );
		$this->_modelRoles->deleteRoleById($roleId);
		js_alert ( '删除成功', null, url ('adminroles') );
	}

}
